package com.techsoul.module.system.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.log.annotation.Log;
import com.techsoul.common.log.enums.OperationTypeEnum;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.system.pojo.param.UserSaveParam;
import com.techsoul.module.system.pojo.param.UserSearchParam;
import com.techsoul.module.system.pojo.param.UserWeChatSaveParam;
import com.techsoul.module.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 用户 Controller
 *
 * @author liuen
 * @date 2020-12-04
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private Result result;
    
    @Autowired
    private IUserService service;
    @Autowired
    private TokenService tokenService;
    @ApiOperation("新增")
    @Log(title = "用户", operationType = OperationTypeEnum.INSERT)
    @PreAuthorize("hasAnyAuthority('system:user:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody UserSaveParam params) {
        return result.success(this.service.insert(params));
    }


    @ApiOperation("本人详情")

    @GetMapping("/getMyInfor")
    public Result getMyInfor() {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        UserVo detaileduser = this.service.findById(user.getId());
        return result.success(detaileduser);
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @Log(title = "用户", operationType = OperationTypeEnum.DELETE)
    @PreAuthorize("hasAnyAuthority('system:user:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }


    @ApiOperation("修改")
    @Log(title = "用户", operationType = OperationTypeEnum.UPDATE)
    @PreAuthorize("hasAnyAuthority('system:user:update')")
    @PutMapping
    public Result update(@Valid @RequestBody UserSaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("修改角色岗位")
    @PreAuthorize("hasAnyAuthority('system:user:update')")
    @Log(title = "用户", operationType = OperationTypeEnum.UPDATE)
    @PutMapping("updateRolePost")
    public Result updateRolePost(@Valid @RequestBody UserSaveParam params) {
        this.service.updateRolePost(params);
        return result.success();
    }

    @PostMapping(value = "/insertWebUser")
    @Log(title = "用户", operationType = OperationTypeEnum.INSERT)
    public Result insertWebUser(@Valid @RequestBody UserSaveParam params){
        return result.success(service.insertWebUser(params));
    }

    @PostMapping(value = "/proveAndConfigWebUser")
    @Log(title = "审核配置用户", operationType = OperationTypeEnum.INSERT)
    public Result proveAndConfigWebUser(@Valid @RequestBody UserSaveParam params){
        return result.success(service.proveAndConfigWebUser(params));
    }
    @ApiOperation("修改个人信息")
    @PreAuthorize("hasAnyAuthority('system:user:update')")
    @Log(title = "用户", operationType = OperationTypeEnum.UPDATE)
    @PutMapping("updateProfile")
    public Result updateProfile(@Valid @RequestBody UserSaveParam params) {
        return result.success(this.service.updateProfile(params));
    }

//    @ApiOperation("修改密码")
//    @PreAuthorize("hasAnyAuthority('system:user:update')")
//    @PutMapping("updatePassword")
//    public Result updatePassword(String oldPassword, String newPassword) {
//        return result.success(this.service.updatePassword(oldPassword, newPassword));
//    }
//
//    @ApiOperation("重置密码")
//    @PreAuthorize("hasAnyAuthority('system:user:resetPassword')")
//    @PutMapping("resetPassword")
//    public Result resetPassword(String id, String password) {
//        return result.success(this.service.resetPassword(id, password));
//    }

//    @ApiOperation("绑定微信")
//    @PostMapping(value = "/bindingWeChat")
//    public Result bindingWeChat(@RequestBody UserWeChatSaveParam params) {
//        service.bindingWeChat(params);
//        return result.success();
//    }
//
//    @ApiOperation("解绑微信")
//    @PostMapping(value = "/unBindingWeChat/{id}")
//    public Result unBindingWeChat(@PathVariable("id") String id) {
//        service.unBindingWeChat(id);
//        return result.success();
//    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('system:user:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {

        return result.success(this.service.findById(id));
    }


    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('system:user:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody UserSearchParam params) {
        return result.success(this.service.findAll(params));
    }

    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('system:user:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody UserSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(this.service.findAll(params, current, pageSize));
    }

    @ApiOperation("列表-角色用户")
    @PreAuthorize("hasAnyAuthority('system:user:search')")
    @PostMapping("/search/roleUsers")
    public Result findRoleUsers(@RequestBody UserSearchParam params) {
        return result.success(this.service.findRoleUsers(params.getRoleId()));
    }

    @ApiOperation("导入模板")
    @PreAuthorize("hasAnyAuthority('system:user:import')")
    @PostMapping("/importTemplate")
    public Result importTemplate() {
        return result.success(this.service.importTemplate());
    }

    @ApiOperation("导入")
    @PreAuthorize("hasAnyAuthority('system:user:import')")
    @Log(title = "用户", operationType = OperationTypeEnum.INSERT)
    @PostMapping("/importData")
    public Result importData(MultipartFile file, boolean isUpdateSupport) {
        return result.success(this.service.importData(file, isUpdateSupport));
    }

    @ApiOperation("导出")
    @PreAuthorize("hasAnyAuthority('system:user:export')")
    @PostMapping("/exportData")
    public Result exportData(@RequestBody UserSearchParam params) {
        return result.success(this.service.exportData(params));
    }

}
